using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CustomerPage_ViewCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lbMess.Text = "";
        if (Session["Cart"] == null)
        {
            MultiView1.ActiveViewIndex = 1;
        }
        else
        {
            if (!IsPostBack)
            {
                gvCart.DataSource = (DataTable)Session["Cart"];
                gvCart.DataBind();
                loadForm();
                btEmpty.Attributes.Add("onclick", "return confirm('Are you Sure you want to empty your cart??')");
            }
        }
    }
    void loadForm()
    {
        DataTable dt = (DataTable)Session["Cart"];
        if (dt == null || dt.Rows.Count == 0)
        {
            return;
        }
        float totalSecurityDeposit, discountSecurityDeposit;
        int countPlan;
        calculateDiscount(dt,out  totalSecurityDeposit,out  discountSecurityDeposit,out countPlan);
        lbSecurityDeposit.Text = totalSecurityDeposit.ToString();
        lbdiscountSecurityDeposit.Text = discountSecurityDeposit.ToString();
        float total=totalSecurityDeposit-discountSecurityDeposit;
        lbTatalSecurityDeposit.Text = total.ToString();

        lbCountPlan.Text = countPlan.ToString();
    }
    void calculateDiscount(DataTable dt, out float totalSecurityDeposit, out float discountSecurityDeposit,out int countPlan)
    {
        totalSecurityDeposit = 0;
        countPlan = 0;
        discountSecurityDeposit = 0;
        foreach (DataRow dr in dt.Rows)
        {
            totalSecurityDeposit += (float)dr["totalSecurityDeposit"];
            countPlan += (int)dr["Amount"];
        }
        if (countPlan < 10)
        {
            discountSecurityDeposit = 0;
        }
        else if (countPlan >= 10 && countPlan <= 16)
        {
            discountSecurityDeposit=totalSecurityDeposit*0.25f;
        }else if(countPlan >16 && countPlan <= 25)
        {
            discountSecurityDeposit = totalSecurityDeposit * 0.5f;
        }
        else if (countPlan > 25 && countPlan <= 50)
        {
            discountSecurityDeposit = totalSecurityDeposit * 0.75f;
        }
        else if (countPlan > 50)
        {
            discountSecurityDeposit = totalSecurityDeposit;
        }
    }
    protected void btUpdate_Click(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)Session["Cart"];
        if (dt == null || dt.Rows.Count == 0)
        {
            MultiView1.ActiveViewIndex = 1;
            return;
        }
        foreach (GridViewRow gr in gvCart.Rows)
        {
            CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbRemove");
            if (cb != null)
            {
                int ID = int.Parse(gr.Cells[1].Text);
                if (cb.Checked)
                {                   
                    removeCart(ID, dt);
                }
                else
                {
                    int amount = int.Parse(((TextBox)gr.Cells[4].FindControl("txtAmount")).Text);
                    if (amount > 0)
                    {
                        UpdateAmountCart(ID, amount, dt);
                    }
                    else
                    {
                        removeCart(ID, dt);
                    }
                }
            }
        }
        if (dt.Rows.Count==0)
        {
            MultiView1.ActiveViewIndex = 1;
            return;
        }
        gvCart.DataSource = dt;
        gvCart.DataBind();
        loadForm();
        lbMess.Text = "has Updated!!";
    }
    void removeCart(int ID, DataTable dt)
    {
        foreach (DataRow dr in dt.Rows)
        {
            if (((int)dr["ID"]) == ID)
            {
                dt.Rows.Remove(dr);
                break;
            }
        }
    }

    void UpdateAmountCart(int ID, int amount, DataTable dt)
    {
        foreach (DataRow dr in dt.Rows)
        {
            if (((int)dr["ID"]) == ID)
            {
                dr["Amount"] = amount;
                float f = (float)dr["SecurityDeposit"];
                dr["totalSecurityDeposit"] = f * amount;
                break;
            }
        }
    }
    protected void btEmpty_Click(object sender, EventArgs e)
    {
        Session["Cart"] = null;
        MultiView1.ActiveViewIndex = 1;
    }
}
